package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.ChartQueryObj;
import cn.wolfcode.crm.service.IOfficialStudentService;
import cn.wolfcode.crm.util.JsonUtil;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chartController")
public class ChartController {

    @Autowired
    private IOfficialStudentService officialStudentServicel;

    @RequestMapping("")
    public String index(){

        return "classOrderChart/list";
    }

    //班级分组查询 已付总款 未付总款
    @RequestMapping("list")
    @ResponseBody
    public PageResult classOrder(ChartQueryObj a){
        PageResult result = officialStudentServicel.classOrder(a);
        return result;
    }

    @RequestMapping("ChartByBar")
    public String saleChartByBar(Model model){
        //副标题  动态处理
        //model.addAttribute("groupType",Dictionary.SALE_GROUPTYPE.get(qo.getGroupType()));
        //sql拿到总数据
        List<Map<String, Object>> listMap = officialStudentServicel.selectSaleChart();
        List<Object> x = new ArrayList<>();
        //销售总额
        List<Object> y = new ArrayList<>();
        for (Map<String, Object> entity : listMap) {
            x.add(entity.get("className"));
            y.add(entity.get("noTotalAmount"));
        }
        //注意: 页面echart接受的json数据
        model.addAttribute("x",JsonUtil.obj2json(x));
        model.addAttribute("y",JsonUtil.obj2json(y));

        return "classOrderChart/ChartByBar";
    }

    @RequestMapping("ChartByPie")
    public String saleChartByPie(Model model){

        //sql拿到总数据
        List<Map<String, Object>> listMap = officialStudentServicel.selectSaleChart();

        //左边的标示: 数据分类  legenddata 格式是一个数组
        List<Object> legenddata = new ArrayList<>();

        //具体显示饼图数据: series.data   格式: [{},{}]
        // [{value=335,name='360'},{value=310,name='华为'}]
        List<Object> seriesdata = new ArrayList<>();
        for (Map<String, Object> entity : listMap) {
            legenddata.add(entity.get("className"));
            //设置显示数据格式
            Map<String,Object> data = new HashMap<>();
            data.put("name",entity.get("className"));
            data.put("value",entity.get("noTotalAmount"));
            seriesdata.add(data);
        }
        //注意: 页面echart接受的json数据
        model.addAttribute("legenddata",JsonUtil.obj2json(legenddata));
        model.addAttribute("seriesdata",JsonUtil.obj2json(seriesdata));

        return "classOrderChart/ChartByPie";
    }



}
